<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { getInfoApi } from '@/api/warehouse/warehouse'
import { addApi } from '@/api/warehouse/warehouseArea'
const dialogVisible = ref(false)
const dialogFormData = ref({
  name: null,
  warehouseName: null,
  warehouseId: null,
  categoryId: null,
  saasId: null,
  isDel: '0'
})
const cateGoryList = ref([
  { name: '收货', value: 'RECEIVING' },
  { name: '储存', value: 'STORAGE' },
  { name: '发货', value: 'SHIPPING' },
  { name: '冷藏区', value: 'COLD_STORAGE' },
  { name: '常温区', value: 'STORAGE' },
])
const resetForm = () => {
  dialogVisible.value = false
  dialogFormData.value = {
    name: null,
    warehouseName: null,
    warehouseId: null,
    category: null,
    saasId: null,
    isDel: '0'
  }
}
// 处理仓库选择变化
const handleWarehouseChange = (val) => {
  // 在 warehouseList 中查找选中的仓库对象
  const selectedWarehouse = warehouseList.value?.find(warehouse => warehouse.name === val)
  if (selectedWarehouse) {
    // 设置 warehouseId 
    dialogFormData.value.warehouseId = selectedWarehouse.id
  } else {
    // 如果没有选中或清空选择，重置 warehouseId
    dialogFormData.value.warehouseId = null
  }
}
const confirm = async () => {
  let res = null;
  if (dialogFormData.value.id) {
    res = await updateApi(dialogFormData.value)
  } else {
    res = await addApi(dialogFormData.value)
  }
  if (res.code) {
    ElMessage.success('操作成功!')
    resetForm()
    instance.emit('search')
  } else {
    ElMessage.error('操作失败！')
  }
}
const warehouseList = ref()
const getWarehouse = async () => {
  let res = await getInfoApi()
  warehouseList.value = res
}
defineExpose({
  dialogFormData,
  dialogVisible,
  getWarehouseList: getWarehouse
})
</script>
<template>
  <el-dialog v-model="dialogVisible" style="width: 400px;">
    <el-form ref="ruleFormRef" :model="dialogFormData" :rules="rules">
      <el-row>
        <el-col span="24">
          <el-select v-model="dialogFormData.warehouseName" placeholder="仓库名称" style="width: 300px;"
            @change="handleWarehouseChange">
            <el-option v-for="warehouse in warehouseList" :label="warehouse.name" :value="warehouse.name" />
          </el-select>
        </el-col>
      </el-row>
      <br />
      <el-row>
        <el-col span="24">
          <el-form-item prop="linkName">
            <el-input v-model="dialogFormData.name" placeholder="库区名称" style="width: 300px;" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col span="24">
          <el-select v-model="dialogFormData.category" placeholder="库区类型" style="width: 300px;">
            <el-option v-for="category in cateGoryList" :label="category.name" :value="category.value" />
          </el-select>
        </el-col>
      </el-row>
      <el-form-item prop="linkPhone" label="是否生效">
        <el-switch v-model="dialogFormData.isDel" active-value='0' inactive-value='1' />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="resetForm">取消</el-button>
        <el-button type="primary" @click="confirm">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>